import Vue from 'vue'
import md5 from 'js-md5'
import store from '../../store'
import router from '../../router'
import { ConfirmPlugin, ToastPlugin } from 'vux'
import { delCookie } from './cookie'
Vue.prototype.md5 = md5
Vue.use(ConfirmPlugin)
Vue.use(ToastPlugin)

/**
 * 接口调用token
 *
 */
export function params(index) {
    let params = new URLSearchParams()
    let time = JSON.stringify(new Date().getTime())
    let token = md5(time + '4baea6bc1bd66a7e00aa778d4aca6720')
    params.append('timeStamp', time)
    params.append('token', token)
    if(index == 1) params.append('uid', store.state.bfUserInfo.uid)
    return params
}

/**
 * toast弹出框 text为弹出内容，time为弹出时间，position为弹出位置
 *
 */
export function toast(text, time, position) {
    this.$vux.toast.show({
        type: 'text',
        isShowMask: false,
        text: text,
        time: time,
        position : position || 'middle'
    });
    let _this = this
    setTimeout(function(){
        _this.$vux.toast.hide()
    }, time)
}

/**
 * 关闭toast弹出框
 *
 */
export function toastClose() {
    this.$vux.toast.hide()
}

/**
 * alert弹出框 content为弹出内容
 *
 */
export function alertVux(content, text) {
    this.$vux.confirm.show({
        // 组件除show外的属性
        content: content, 
        confirmText: text, 
        showCancelButton: false,
        hideOnBlur: true,
        onCancel () {
        },
        onConfirm () {
        }
    })
}

/**
 * 登录失效判断
 *
 */
export function logAgain(msg) {
    if(msg == '请先登录'){
        const _this = this // 需要注意 onCancel 和 onConfirm 的 this 指向
        this.$vux.confirm.show({
            // 组件除show外的属性
            content: '当前登录已失效，请重新登录',
            confirmText: '去登录',
            showCancelButton: false,
            hideOnBlur: true,
            onCancel () {
            },
            onConfirm () {
                _this.$router.push('/login')
            }
        })
        delCookie('userInfo') 
        sessionStorage.clear()
        // 更新用户信息
        store.commit('SET_USERINFO')
    }else if(msg){
        this.$toast(msg, 2000)
    }else{
        this.$toast('请求超时', 2000)
    }
} 

/**
 * 检查内容是否填写
 *
 */
export function checkWrite(arr, type) {
    let status = false
    for(let i = 0; i <arr.length; i++){
        if(!isDefine(arr[i].value)){
            if(type == 'toast'){
                this.$toast(arr[i].placeholder, 2000)
            }else{
                this.$alert(arr[i].placeholder)
            }
            status = true
            return status
        }
    }
    return status
}



const getClass = (name) => document.getElementsByClassName(name)

/**
 * 查询是否有值 有返回true
 *
 */
const isDefine = (value) => {
    if (value == null || value == '' || value == 'undefined' || value == undefined || value == 'null' || value == '(null)' || value == 'NULL' || typeof (value) === 'undefined') {
        return false
    }
    return true
}

/**
 * 查询是否空对象 是返回true
 *
 */
const isEmptyObject = (value) => {
    if(value.length > 0){
        for (let name in value){ 
            return false
        } 
    }
    return true
}


/**
 * 彩票下注/取消下注
 *
 */
const choose = (playType0, title, data, odds) => {
    // 选中拖头ID/普通ID/数据/彩票种类
    let {lotOptTra, lotOptions, lotSelData, lotType, lotTitle} = store.state
    let id = playType0 + title[0] + data[2] + data[3]
    // 选中玩法
    let playType1 = data[2]
    if(title[0].indexOf('zhix')>-1){
        playType1 = playType0 + title[0]
    }else if(isDefine(playType0)){
        playType1 = playType0
    }else if(playType1 == ''){
        playType1 = title[0]
    }else if(playType1.indexOf('_') > -1){ // 大小单双
        playType1 = title[0] + playType1
    }
    // 选中name
    let name = data[0]
    let palyArr = ['xglhc', 'bflhc', 'gdsyxw', 'gdklsf']
    if(lotTitle == '和数'){ // 和数玩法name需单独截取
        name = title[1].substr(0, 2) + name
    }else if(isDefine(title[1]) && palyArr.indexOf(lotType) < 0 || lotTitle == '过关'){
        // 若内容为数字则添加‘-’显示
        let contType = isNaN(Number(name)) ? '' : '-'
        name = title[1] + contType + name
    }
    // 选中数据重组
    let selObj = {
        'odds' : odds,
        'playType' : playType1,
        'content' : data[3],
        'contN' : data[4],
        'name' : name,
        'nameT' : palyArr.indexOf(lotType) > -1 ? title[1] : ''
    }
    // 判断是否为已选项
    if(lotOptions.indexOf(id) > -1){ // 已选则取消选中
        lotOptions = lotOptions.filter(n => n !== id)
        // 查找取消项数据下标并清除数据 
        for (var i = 0; i < lotSelData.length; i++) { 
            if(JSON.stringify(lotSelData[i]) == JSON.stringify(selObj)) lotSelData.splice(i, 1)
        } 
    }else{ // 未选则添加选项
        if(title[0].indexOf('zhix') > -1){ // 11选5-直选
            let lotArr = []
            for(let i=0; i<lotOptions.length; i++){
                lotArr[i] = lotOptions[i].slice(10)
            }
            // 不能选择已选号码
            const sel = id.slice(10)
            if(lotArr.indexOf(sel) < 0){
                lotOptions.push(id) // 选中id
                lotSelData.push(selObj) // 选中数据
            }
        }else if(store.state.lhcTra == -1){ // 普通、复式
            // 添加选项
            lotOptions.push(id) // 选中id
            lotSelData.push(selObj) // 选中数据
        }else if(store.state.lhcTra == 1){ // 拖头
            // 拖头玩法单独处理
            const tra = chooseTra(lotOptTra, lotOptions, lotSelData, id, selObj)
            lotOptTra = tra[0]
            lotOptions = tra[1]
            lotSelData = tra[2]
        }else{
            // 连码玩法单独处理
            const lm = chooseLm(lotOptions, lotSelData, id, selObj)
            lotOptions = lm[0]
            lotSelData = lm[1]
        }
    }
    store.state.lotOptTra = lotOptTra // 选中id
    store.state.lotOptions = lotOptions // 选中id
    store.state.lotSelData = lotSelData // 选中数据
}

/**
 * 彩票连码对碰模式下注/取消下注
 *
 */
const chooseLm = (lotOptions, lotSelData, id, selObj) => {
    if(isEmptyObject(lotOptions)){ // 未选中
        lotOptions.push(id) // 选中id 尾
        lotSelData.push(selObj) // 选中数据
    }else{
        let sign = [] //数据处理标识
        let data1 = [] // 替换数据
        let data2 = [] // 添加数据
        for(let i=0; i<lotOptions.length; i++){
            const sid = id.split('_')[3].slice(3)
            const oid = lotOptions[i].split('_')[3].slice(3)
            const sel = id.split('_')[3].substr(0, 3)
            const old = lotOptions[i].split('_')[3].substr(0, 3)
            if(sid == oid){ // 内容相同
                sign.push(-1)
            }else if(sel == old){ // 分组相同
                if(store.state.lhcTra == 5){ // 任意对碰
                    data2 = [i, id, selObj]
                }else{ // 其他玩法
                    sign.push(0)
                    data1 = [i, id, selObj]
                }
            }else if(sel != old){ // 都不相同
                sign.push(1)
                data2 = [i, id, selObj]
            }
        }
        if(sign.indexOf(-1) > -1){ // 内容相同不做处理
            return [lotOptions, lotSelData]
        }else if(sign.indexOf(0) > -1){ // 分组相同替换数据
            lotOptions.splice(data1[0], 1, data1[1])
            lotSelData.splice(data1[0], 1, data1[2])
        }else{ // 都不相同添加数据
            lotOptions.push(data2[1]) // 选中id 尾
            lotSelData.push(data2[2]) // 选中数据
        }
    }
    return [lotOptions, lotSelData]
}

/**
 * 彩票拖头模式下注/取消下注
 *
 */
const chooseTra = (lotOptTra, lotOptions, lotSelData, id, selObj) => {
    const { lhcPlay } = store.state // 组合玩法类型
    // 判断是否为已选项
    if(lotOptTra.indexOf(id) > -1){ // 已选则清空选中 拖头
        // 重置下注数据
        lotOptTra = []
        lotOptions = []
        lotSelData = []
    }else{ // 未选则添加选项
        if(lotOptTra.length < lhcPlay-1){
            lotOptTra.push(id) // 选中id 头
        }else{
            lotOptions.push(id) // 选中id 尾
        }
        lotSelData.push(selObj) // 选中数据
    }
    return [lotOptTra, lotOptions, lotSelData]
}
 
/**
 * 彩票打开订单页面
 *
 */
const openLotOrder = (money) => {
    const { lotSelData } = store.state
    const { min, max } = store.state.lotData
    for(let i=0; i<lotSelData.length; i++){
        lotSelData[i].payPrice = money || min
    }
    router.push({
        path: '/lotteryOrder',
        query: {
            inputPrice : money || min,
            payPriceMin : min,
            payPriceMax : max
        }
    })
}

export { getClass, isDefine, isEmptyObject, choose, chooseTra, openLotOrder }

